package org.cuatrovientos.erp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.RegisterBOInterface;
import org.cuatrovientos.erp.model.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @author CARLOS
 *
 */
public class RegisterController {


	// Get log4j logger
	private static final Logger logger = Logger.getLogger(RegisterController.class);

	@Autowired
    private RegisterBOInterface registerBO;
    
        /**
         * default constructor
         */
        public RegisterController () {
               logger.info("Register BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /shipment or /shipment/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping({"/register","/register/list"})
        public ModelAndView registerDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Register> registerList = registerBO.selectAll();
                model.put("registers", registerList );
                return new ModelAndView("registerList", model);     
        }

        /**
         * handles /shipment/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/register/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Register> registerList = registerBO.search(description);
            model.put("registers", registerList );
            
            return new ModelAndView("RegisterList", model);     
        }

        /**
         * handles delete shipment, and redirect to shipment default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/register/delete"})
        public String deleteRegister(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            registerBO.deleteById(id);
            return "redirect:/register";

        }
        
        /**
         * handles new shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "shipment"
         * @return the name of the view to show
         * @RequestMapping({"/shipment/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/register/new"})
        public ModelAndView newRegister() {
        		logger.info("new register ");
                Register register = new Register();
               
                return new ModelAndView("NewRegister", "register", register);
        }
        
        /**
         * handles new shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/shipment/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/register/save"})
        public String newRegister(@Valid Register register, BindingResult bindingResult) {
        		logger.info("new register " + register.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("register", register);
        			return "NewRegister";
        		}
        		
        		// If data is ok, insert and go on
                registerBO.insert(register);
                return "redirect:/register";
        }
        
        /**
         * handles delete shipment, and redirect to shipment default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment","/shipment/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/register/detail"})
        public ModelAndView registerDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Register register = registerBO.selectById(id);
            model.put("register", register);
            
            return new ModelAndView("RegisterDetail",model);

        }
        
        /**
         * handles shipment update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/shipment/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/register/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Register register = registerBO.selectById(id);
                model.put("register", register);
               return new ModelAndView("UpdateRegister",model);

        }
        
        /**
         * handles save update shipment request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/shipment/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/register/saveupdate"})
        public String saveUpdate(@Valid Register register, BindingResult bindingResult) {
        		logger.info("update register " + register.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("register", register);
        			return "UpdateRegister";
        		}
        		
                registerBO.update(register);
                return "redirect:/register";
        } 
    	/**
    	 * @param model
    	 * @return
    	 */
    	@RequestMapping({"/register/index"})
    	public String sayHelloPage(Map<String, Object> model) {
    		return "index/IndexRegister";
    	}
}
